/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.level;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Image;
import towerz.game.Sprite;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class Wave {
	protected Wave() {
		//initialize
		m_EnemyMax		= 0;
		m_EnemyCount	= 0;
		m_SpawnTimer	= 0;
		m_SpawnDelay	= 0;
		m_EnemyData		= null;
		m_EnemyImages	= null;
	}
	public Wave(XmlNode xml) {
		//Default
		this();

		//For each node
		for(int i=0; i < xml.getChildren().size(); ++i){
			//Get tag name and it's value
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();
			String Value	= ((XmlNode)((XmlNode) xml.getChildren().elementAt(i)).getChildren().elementAt(0)).getNodeValue();

			//Set data
			if (Tag.equals("bonus"))		m_Bonus = Integer.parseInt(Value);
			else if (Tag.equals("count"))	m_EnemyMax = Integer.parseInt(Value);
			else if (Tag.equals("delay"))	m_SpawnDelay = Integer.parseInt(Value);
			else if (Tag.equals("data"))	m_EnemyData = (XmlNode)xml.getChildren().elementAt(i);
			else if (Tag.equals("image"))	m_EnemyFile	= Value;
		}
	}

	public void loadEnemyImage() {
		//Set data
		int Row		= 1;
		int Column	= 4;

		//Create image array
		int[] Tints		= Sprite.getTints();
		m_EnemyImages	= new Image[4][Tints.length][Row * Column];

		try {
			//For each direction
			for (int j = 0; j < 4; j++) {
				//Get image data
				Image Source	= Image.createImage(Utilities.ENEMY_FOLDER + "/" + m_EnemyFile + "_" + j + ".png");
				int Width		= Source.getWidth()	 / Column;
				int Height		= Source.getHeight() / Row;

				//For each frame
				Image[] Images = Utilities.splitTransparentImage(Source, Width, Height);
				for (int l = 0; l < m_EnemyImages[j][0].length; l++) {
					m_EnemyImages[j][0][l] = Images[l];

					//For each tint
					for (int k = 1; k < Tints.length; k++)
						m_EnemyImages[j][k][l] = Utilities.createTintedImage(m_EnemyImages[j][0][l], Tints[k]);
				}
			}
		} catch (IOException ex) {}
	}

	public void unloadEnemyImage() {
		//Destroy image
		m_EnemyImages = null;
	}

	public int getBonus()				{	return m_Bonus;													}
	public Image[][][] getEnemyImages()	{	return m_EnemyImages;											}
    public boolean isWaveFinished()		{   return isUnlimited() ? false : (m_EnemyCount >= m_EnemyMax);	}
	public boolean isUnlimited()		{	return m_EnemyMax < 0;											}

	public XmlNode[] spawnEnemy(long time) {
		//Default value
		XmlNode[] Spawn = null;
		Vector Spawns	= new Vector();
		
		//If time minus
		if (time < 0) { 
			//Spawn one
			m_EnemyCount++;
			Spawn = new XmlNode[] { m_EnemyData	};
		} else if (m_EnemyMax < 0 || m_EnemyCount < m_EnemyMax) {
			//Increase timer
			m_SpawnTimer += time;
			
			//If delayed enough
			if (m_SpawnTimer >= m_SpawnDelay) {
				//While more
				while (m_SpawnTimer > m_SpawnDelay && (m_EnemyCount < m_EnemyMax || m_EnemyMax < 0)) {
					//Add
					Spawns.addElement(m_EnemyData);
					m_EnemyCount++;

					//Reduce timer
					m_SpawnTimer -= m_SpawnDelay;
				}

				//Transfer to array
				Spawn = new XmlNode[Spawns.size()];
				for (int i = 0; i < Spawns.size(); i++) Spawn[i] = (XmlNode)Spawns.elementAt(i);
			}
		}

		//Return spawned
		return Spawn;
	}

	//Member
	protected int			m_Bonus;
	protected int			m_EnemyMax;
	protected int			m_EnemyCount;
	protected long			m_SpawnTimer;
	protected long			m_SpawnDelay;
	protected XmlNode		m_EnemyData;
	protected String		m_EnemyFile;
	protected Image[][][]	m_EnemyImages;
}
